博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django doc summary (4)
阅读量:4317 次
发布时间:2019-06-06

本文共 3783 字,大约阅读时间需要 12 分钟。

View

In Django, each view is represented by a simple python function.

To get from a URL to view, Django uses what are know as 'URLconfs'. A URL conf maps URL patterns to views.

Now we edit the samapp views.py to add three functions.They are 'detail', 'results' and vote'.

def detail(request, question_id):    return HttpResponse("You're looking at question %s." %question_id)def results(request, question_id):    response = "You're looking at the results of question %s."    return HttpResponse(response % question_id)def vote(request, question_id):    return HttpResponse("You're voting on question %s." % question_id)

Then we should edit the 'samapp/urls.py' file to take the three functions in action.

# /samapp/url(r'^$', views.index, name='index'),# /samapp/23/url(r'^(?P
[0-9]+)/$', views.detail, name='detail'),# /samapp/23/results/url(r'^(?P
[0-9]+)/results/$', views.results, name='results'),# /samapp/23/vote/ url(r'^(?P
[0-9]+)/vote/$', views.vote, name='vote'),

A view that actually do something

views.py

def index(request):    latest_question_list = Question.objects.order_by('-pub_date')[:5]    output = ', '.join([q.question_teext for q in latest_question_list])    #return HttpResponse("Hello, this is sam site.")    return HttpResponse(output)

Template system

Next, let's use Django's template system to separate the design from python by creating a template that the view can use.

The template file position should like below.

├─samapp│  ││  ├─templates│  │  └─samapp│  │          index.html

index.html

{% if latest_question_list %}    
{% else %}

No polls are available.

{% endif %}

Then, we update the samapp/views.py to use the template.

def index(request):    latest_question_list = Question.objects.order_by('-pub_date')[:5]    template = loader.get_template('samapp/index.html')    context = RequestContext(request, {'latest_question_list': latest_question_list})    return HttpResponse(template.render(context))

A shortcut: render()

from django.shortcuts import renderfrom .models import Questiondef index(request):    latest_question_list = Question.objects.order_by('-pub_date')[:5]    context = {'latest_question_list': latest_question_list}    return render(request, 'polls/index.html', context)

A shortcut: get_object_or_404()

from django.shortcuts import get_object_or_404, renderfrom .models import Questiondef detail(request, question_id):    question = get_object_or_404(Question, pk=question_id)    return render(request, 'polls/detail.html', {'question': question})

Removing hardcoded URLs in templates

As before, in the template, the link war partially hardcoded link this:

    {
{ question.question_text }}

That is tightly-coupled approached with url '/samapp/'.So, we'd better do like this.

  • {
    { question.question_text }}
  • This will look up the URL definition in the samapp/urls.py file. And if you want to change the URL, you would change only int the samapp/urls.py file.

    Namespacing URL names

    When we have more than one apps, we will encounter a problem.

    The url name may be the same. So, we should add namespaces to our URL conf.

    from django.conf.urls import urlapp_name = 'polls'urlpatterns = [    url(r'^$', views.index, name='index'),    url(r'^(?P
    [0-9]+)/$', views.detail, name='detail'), url(r'^(?P
    [0-9]+)/results/$', views.results, name='results'), url(r'^(?P
    [0-9]+)/vote/$', views.vote, name='vote'),]

    Now, change the template from the origin to the below.

  • {
    { question.question_text }}
  • 转载于:https://www.cnblogs.com/samrui/p/5037573.html

    你可能感兴趣的文章
    softmax
    查看>>
    Newtonsoft 和 FastJson 的序列化和反序列化性能的对比
    查看>>
    caffe——网络参数转化
    查看>>
    【读书】《麦肯锡不外传的简报形式》
    查看>>
    JS正则[egExp]小记
    查看>>
    javascript面向对象(三)
    查看>>
    黑马程序员-IO流(对象序列化、RandomAccessFile 类、字符编码、综合练习)
    查看>>
    牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
    查看>>
    (转)AS3 面相对象 高级话题
    查看>>
    Missile
    查看>>
    关于kindedit和 Uedit后者兼容前者
    查看>>
    微软BI 之SSIS 系列 - 利用 SSIS 模板快速开发 SSIS Package
    查看>>
    eclipse中使用git上传到githup,报401 Authorization Required
    查看>>
    基于Golang打造一款开源的WAF网关
    查看>>
    POJ 2955 Brackets
    查看>>
    Python: execute an external program (zz)
    查看>>
    在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
    查看>>
    Ubuntu14.04安装JDK
    查看>>
    Latex 公式换行问题(换行,等号对齐)
    查看>>
    php mysqli解决乱码
    查看>>